<?php

namespace app\api\service\weixin;

use app\api\service\Memberlevel as MemberlevelService;

use think\Db;
use think\Exception;
use think\Loader;
use think\Log;

Loader::import('WxPay.WxPay',EXTEND_PATH,'.Api.php');

/**
 * 升级会员等级支付回调
 */
class MemberlevelNotify extends \WxPayNotify
{

    //<xml>
    //<appid><![CDATA[wx2421b1c4370ec43b]]></appid>
    //<attach><![CDATA[支付测试]]></attach>
    //<bank_type><![CDATA[CFT]]></bank_type>
    //<fee_type><![CDATA[CNY]]></fee_type>
    //<is_subscribe><![CDATA[Y]]></is_subscribe>
    //<mch_id><![CDATA[10000100]]></mch_id>
    //<nonce_str><![CDATA[5d2b6c2a8db53831f7eda20af46e531c]]></nonce_str>
    //<openid><![CDATA[oUpF8uMEb4qRXf22hE3X68TekukE]]></openid>
    //<out_trade_no><![CDATA[1409811653]]></out_trade_no>
    //<result_code><![CDATA[SUCCESS]]></result_code>
    //<return_code><![CDATA[SUCCESS]]></return_code>
    //<sign><![CDATA[B552ED6B279343CB493C5DD0D78AB241]]></sign>
    //<sub_mch_id><![CDATA[10000100]]></sub_mch_id>
    //<time_end><![CDATA[20140903131540]]></time_end>
    //<total_fee>1</total_fee>
    //<trade_type><![CDATA[JSAPI]]></trade_type>
    //<transaction_id><![CDATA[1004400740201409030005092168]]></transaction_id>
    //</xml>
    public function NotifyProcess($data, &$msg){
        if ($data['result_code'] == 'SUCCESS') {

            Db::startTrans();
            try {
                $orderNo = $data['out_trade_no'];
                $paytime = self::timeFormat($data['time_end']);

                $model = new \app\admin\model\Memberlevelrecord();
                $order = $model->where('order_no', $orderNo)->find();
                if (!$order) {
                    Log::record('订单不存在');
                    return false;
                }

                if ($order->status != 1){
                    $param = new \stdClass();
                    $param->memberId = $order->member_id;
                    $param->levelId = $order->member_level_id;
                    $param->vip_cost = $order->money;
                    $param->vip_time = $order->level_time;
                    $param->pay_method = $order->pay_method;
                    // 开通成功后更新会员相关信息
                    $result = MemberlevelService::updateMember($param);
                    if ($result['code'] != 1) {
                        Log::record($result['msg']);
                        return false;
                    }

                    // 更新订单状态
                    $result = $order->where('order_no', $orderNo)->update(['status' => 1, 'pay_date' => $paytime]);
                    if($result === false){
                        Log::record('更新订单状态失败');
                        return false;
                    }
                }

                Db::commit();
                return true;
            } catch (Exception $e) {
                Db::rollback();
                Log::record($e->getMessage());
                return false;
            }
        } else {
            return false;
        }
    }

    public static function timeFormat($time){
        $paytime = substr($time,0,4).'-';
        $paytime .= substr($time,4,2).'-';
        $paytime .= substr($time,6,2);
        $paytime .= ' ';
        $paytime .= substr($time,8,2).':';
        $paytime .= substr($time,10,2).':';
        $paytime .= substr($time,12,2);
        $paytime = date('Y-m-d H:i:s',strtotime($paytime));

        return $paytime;
    }
}